package com.csyf.manager.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.*;
import java.util.Map.Entry;


@Slf4j
public class SignUtils {

    public static <T> Map<String, String> object2Map(T t) {
        Map<String, String> treeMap = new TreeMap<>();
        Class<?> class1 = t.getClass();
        while (null != class1) {
            Field[] declaredFields = class1.getDeclaredFields();
            try {
                for (Field field : declaredFields) {
                    if ("serialVersionUID".equals(field.getName()) || "sign".equals(field.getName())) {
                        continue;
                    }
                    field.setAccessible(true);
                    Object object = field.get(t);
                    if (null == object) {
                        continue;
                    }
                    treeMap.put(field.getName(), null == object ? null : object.toString());
                }

                class1 = class1.getSuperclass();
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }


        return treeMap;
    }

    public static String objectsign(Object object, String key) {
        Map<String, String> object2Map = object2Map(object);
        return map2signkey(object2Map, key);
    }


    public static String map2signkey(Map<String, String> object, String key) {
        Set<Entry<String, String>> entrySet = object.entrySet();
        ArrayList<String> arrayList = new ArrayList<>();
        for (Entry<String, String> entry : entrySet) {
            if ("sign".equals(entry.getKey())) {
                continue;
            }
            if (null == entry.getValue() || entry.getValue().length() <= 0) {
                continue;
            }
            arrayList.add(entry.getKey() + "=" + entry.getValue());
        }
        arrayList.add("key=" + key);
        String join = StringUtils.join(arrayList, "&");
        log.info("签名字符串:{}", join);
        return DigestUtils.md5Hex(join).toUpperCase();
    }

    public static boolean objectVerifySign(Object object, String key, String sign) {
        Map<String, String> object2Map = object2Map(object);
        return verifySignkey(object2Map, key, sign);

    }

    public static boolean verifySignkey(Map<String, String> map, String key, String sign) {

        String s = map2signkey(map, key);
        log.info("客户签名值:{},系统加密值:{}", sign, s);
        return s.equalsIgnoreCase(sign);
    }


    /**
     * 将  key1=value1&key2=value2&key3=value3 的值转成map
     *
     * @param string
     * @return
     */
    public static Map<String, String> paramStirng2Map(String string) {

        Map<String, String> map = new HashMap<>();
        String[] split = StringUtils.split(string, "&");
        for (String s : split) {
            String[] split1 = StringUtils.split(s, "=", 2);
            if (split1.length > 1) {
                map.put(split1[0], split1[1]);
            } else {
                map.put(split1[0], null);
            }

        }
        return map;
    }
}
